added samples
[windows-sources.git] / sdk / samples / all in on code / Visual Studio 2008 / VBLinqExtension / LinqToString.vb
blobac1296e77fdcad90c116a2331b9a475a1491160c
1 '****************************** Module Header ******************************\
2 ' Module Name: LinqToString.vb
3 ' Project: VBLinqExtension
4 ' Copyright (c) Microsoft Corporation.
6 ' It is a simple LINQ to String library to show the digis characters in a
7 ' string, to count occurrences of a word in a string, and to query for
8 ' sentences that contain a specified set of words.
9 '
10 ' This source is subject to the Microsoft Public License.
11 ' See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
12 ' All other rights reserved.
14 ' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
15 ' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
16 ' WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
17 '***************************************************************************/
19 #Region "Imports directives"
20 Imports System
21 Imports System.Collections.Generic
22 Imports System.Linq
23 Imports System.Text
24 Imports System.Runtime.CompilerServices
26 #End Region
28 Public Module LinqToString
30 ''' <summary>
31 ''' Extension method of string to get the digit characters
32 ''' in a string.
33 ''' </summary>
34 ''' <returns>IEnumerable collection digit characters</returns>
35 '''
36 <Extension()> _
37 Public Function GetDigits(ByVal text As String) As IEnumerable(Of Char)
38 ' Get the chars if it is a digit
39 Dim digits = From ch In text _
40 Where Char.IsDigit(ch) _
41 Select ch
43 Return digits
44 End Function
47 ''' <summary>
48 ''' Extension method of string to count occurrences of a word
49 ''' in a string.
50 ''' </summary>
51 ''' <param name="searchTerm">The search item string</param>
52 ''' <returns>The search item occurences</returns>
53 '''
54 <Extension()> _
55 Public Function GetWordOccurrence(ByVal text As String, ByVal searchTerm As String) As Integer
56 ' Split the source string into single words
57 Dim source As String() = text.Split(New Char() {"."c, "?"c, "!"c, " "c, ";"c, ":"c, _
58 ","c}, StringSplitOptions.RemoveEmptyEntries)
60 ' Query the occurences of the search item
61 Dim matchQueryCount As Integer = (From word In source _
62 Where word.ToLowerInvariant() = searchTerm.ToLowerInvariant() _
63 Select word).Count()
65 Return (matchQueryCount)
66 End Function
68 ''' <summary>
69 ''' Extension method of string to query for sentences that contain
70 ''' a specified set of words.
71 ''' </summary>
72 ''' <param name="wordsToMatch">The search item set</param>
73 ''' <returns>The set of sentences</returns>
74 '''
75 <Extension()> _
76 Public Function GetCertainSentences(ByVal text As String, ByVal wordsToMatch As String()) As IEnumerable(Of String)
77 ' Split the source string into single sentences
78 Dim sentences = text.Split(New Char() {"."c, "?"c, "!"c})
80 ' Query for sentences that contain the set of search itmes
81 Dim sentenceQuery = From sentence In sentences _
82 Let w = sentence.Split(New Char() {"."c, "?"c, "!"c, " "c, ";"c, ":"c, _
83 ","c}, StringSplitOptions.RemoveEmptyEntries) _
84 Where w.Distinct().Intersect(wordsToMatch).Count() = wordsToMatch.Count() _
85 Select sentence
87 Return sentenceQuery
88 End Function
89 End Module